require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'ztree',
    'calendar'
], function ($, common, ready, template, Collection, Model, dialog) {

    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            //loading();
            this.event();
            this.fetch();
        },

        fetch: function (_args) {
            var _this = this;
            var args = _args || {};
            args.limit = 10;
            args.offset = 0;

            _this.collection = new Collection({});
            _this.collection.fetch();

        },

        zTree: function () {
            var setting = {
                async: {
                    enable: true,
                    type: "get",
                    url: _global.url.api + 'user_tree',
                    autoParam: ["id", "name=n", "level=lv"],
                    otherParam: {
                        'mobile': $('input[name=mobile]').val(),
                        "start_time": $('input[name=start_time]').val(),
                        'end_time': $('input[name=end_time]').val()
                    },
                    dataFilter: filter
                },
                callback: {
                    beforeAsync: function (treeId, treeNode) {
                        if (treeNode != undefined) {
                            //获取当前节点的所有父节点,第一个对象即是最顶级的父节点
                            var parent = treeNode.getPath();
                            //获取整个tree的对象
                            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                            //如果tree对象不为null,则将顶级节点的id提交到服务端
                            if (treeObj != null) {
                                treeObj.setting.async.otherParam.pid = parent[0].id;

                                var start_time = $('input[name=start_time]').val();

                                if (start_time !== "" && start_time !== undefined && start_time !== null) {
                                    treeObj.setting.async.otherParam.start_time = start_time;
                                }
                                var end_time = $('input[name=end_time]').val();
                                if (end_time !== "" && end_time !== undefined && end_time !== null) {
                                    treeObj.setting.async.otherParam.end_time = end_time;
                                }

                                var mobile = $('input[name=mobile]').val();
                                if (mobile !== "" && mobile !== undefined && mobile !== null) {
                                    treeObj.setting.async.otherParam.mobile = mobile;
                                } else {
                                    alert('手机号码不能为空');
                                    return false;
                                }
                            }
                        }
                    }
                },
                view: {
                    nameIsHTML: true,
                    expandSpeed: "slow"
                }
            };

            function filter(treeId, parentNode, childNodes) {
                if (!childNodes) return null;
                for (var i = 0, l = childNodes.length; i < l; i++) {
                    childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
                }
                return childNodes;
            }

            $(document).ready(function () {
                $.fn.zTree.init($("#treeDemo"), setting);
            });
        },

        event: function () {
            var _this = this;
            $('.form_datetime').calendar();

            $('#render_tree').on('click', function (e) {
                e.preventDefault();
                var m = $('input[name=mobile]').val();
                if (m === "" || m === undefined || m === null) {
                    alert('手机号码不能为空');
                    return false;
                }
                _this.zTree();
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }

    };

    new index();

});